package temp.lho.lws.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import temp.lho.lws.pojo.Flowinfomation;
import temp.lho.lws.result.ResultBody;
import temp.lho.lws.service.IFlowinfomationService;
import temp.lho.lws.status.FlowStatus;
import temp.lho.lws.vo.LayuiResult;
import temp.lho.lws.vo.LoginVo;

import java.util.List;

/**
 * (Flowinfomation)表控制层
 *
 * @author makejava
 * @since 2020-07-06 23:08:54
 */
@RestController
@RequestMapping("/flowinfomation")
public class FlowinfomationController {

    @Autowired
    private IFlowinfomationService flowinfomationService;

    @GetMapping("/findFlowDetail/{taskname}/{jobstate}")
    public LayuiResult findFlowinfo(@PathVariable String taskname,
                                    @PathVariable Integer jobstate) {
        Subject subject = SecurityUtils.getSubject();
        LoginVo principal = (LoginVo) subject.getPrincipal();
        List<Flowinfomation> flowDetail = flowinfomationService.findFlowDetail(taskname, principal.getUserId(), jobstate);
        return new LayuiResult(0,"",flowDetail.size(),flowDetail);
    }

    @GetMapping("/updateJobState")
    public ResultBody updateJobState(@RequestParam("taskid") Integer taskid,
                                     @RequestParam("jobstate") Integer jobstate,
                                     @RequestParam(value = "transmitstate",required = false) Integer transmitstate) {
        Subject subject = SecurityUtils.getSubject();
        LoginVo principal = (LoginVo) subject.getPrincipal();

        boolean b = flowinfomationService.updateJobStatus(taskid, jobstate, transmitstate, principal);

        return b ? ResultBody.toResultBody(200,"任务已接收"):ResultBody.toResultBody(-1,"任务接收失败");
    }

    @PostMapping("/flowBack")
    public ResultBody flowBack(@RequestBody Flowinfomation flowinfomation) {

        Subject subject = SecurityUtils.getSubject();
        LoginVo principal = (LoginVo) subject.getPrincipal();

        Flowinfomation addFlow = new Flowinfomation();
        addFlow.setTaskpid(flowinfomation.getTaskpid());
        addFlow.setTaskname(FlowStatus.FLOW_LOSS);
        addFlow.setFlowid(flowinfomation.getFlowid());
        addFlow.setClaimmainid(flowinfomation.getClaimmainid());
        addFlow.setAssignedid(principal.getUserId().toString());
        addFlow.setAssignedname(principal.getUsername());
        addFlow.setJobstate(FlowStatus.FLOW_JOB_RETURN);

        boolean b = flowinfomationService.addNextFlow(addFlow);

        return b ? ResultBody.toResultBody(200, "退回成功！") : ResultBody.toResultBody(-1, "退回失败！");

    }

    @PostMapping("/addNextFlow")
    public ResultBody addNextFlow(@RequestBody Flowinfomation flowinfomation) {
        boolean b = flowinfomationService.addNextFlow(flowinfomation);
        return b ? ResultBody.toResultBody(200, ""):ResultBody.toResultBody(-1, "");
    }

}